﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_FIM_MAS_ReceiveTypes : IDAL_FIM_MAS_ReceiveTypes
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, ConfigurationSettings.AppSettings["ConnectionString"].ToString());
        ConvertNullable cn = new ConvertNullable();
        //

        public IList<M_FIM_MAS_ReceiveTypes> GetFull(Nullable<int> bgm_source_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_ReceiveTypes> infoList = new List<M_FIM_MAS_ReceiveTypes>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_source_seq", bgm_source_seq);

                strSQL = "SELECT rt.receive_type_seq, rt.receive_type_name"
                    + ", rt.bgm_source_seq, s.bgm_source_name, rt.bank_acc_seq"
                    + ", concat(acc.bank_acc_no, ' : ', acc.bank_acc_name) as bank_acc_name"
                    + ", rt.account_seq, rt.inv_connect_type, rt.remarks"
                    + ", case when rt.inv_connect_type = '0' then ''"
                        + " else case when rt.inv_connect_type = '1' then 'ขายแบบ'"
                            + " else case when rt.inv_connect_type = '2' then 'หลักประกันซอง'"
                                + " else case when rt.inv_connect_type = '3' then 'หลักประกันสัญญา'"
                                    + " else case when rt.inv_connect_type = '4' then 'หลักประกันจ่ายล่วงหน้า'"
                                        + " else case when rt.inv_connect_type = '5' then 'จำหน่ายพัสดุ'"
                                            + " else 'ไม่ระบุ'"
                                            + " end"
                                        + " end"
                                    + " end"
                                + " end"
                            + " end"
                        + " end as inv_connect_type_name"
                    + ", rt.record_status"
                    + ", rt.created_by, rt.created_when, rt.updated_by, rt.updated_when"
                    + ", rt.temp_income, rt.not_sale"
                    + " FROM fim_mas_receive_types rt"
                    + " LEFT JOIN bgm_mas_source s ON rt.bgm_source_seq = s.bgm_source_seq"
                    + " LEFT JOIN fim_mas_bank_accounts acc ON rt.bank_acc_seq = acc.bank_acc_seq"
                    + " WHERE rt.record_status not like @delete_status"
                    + (bgm_source_seq != null ? " AND rt.bgm_source_seq = @bgm_source_seq" : "")
                    + " ORDER BY rt.bgm_source_seq, rt.receive_type_name";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_ReceiveTypes info = new M_FIM_MAS_ReceiveTypes();
                        info.receive_type_seq = Convert.ToInt32(drc[i]["receive_type_seq"]);
                        info.receive_type_name = drc[i]["receive_type_name"].ToString();
                        info.bgm_source_seq = cn.ConvertNullInt(drc[i]["bgm_source_seq"]);
                        info.bgm_source_name = drc[i]["bgm_source_name"].ToString();
                        info.bank_acc_seq = cn.ConvertNullInt(drc[i]["bank_acc_seq"]);
                        info.bank_acc_name = drc[i]["bank_acc_name"].ToString();
                        info.account_seq = cn.ConvertNullInt(drc[i]["account_seq"]);
                        info.inv_connect_type = drc[i]["inv_connect_type"].ToString();
                        info.remarks = drc[i]["remarks"].ToString();
                        info.inv_connect_type_name = drc[i]["inv_connect_type_name"].ToString();
                        info.temp_income = drc[i]["temp_income"].ToString() == "1";
                        info.not_sale = drc[i]["not_sale"].ToString() == "1";

                        info.record_status = drc[i]["record_status"].ToString();
                        info.created_by = cn.ConvertNullInt(drc[i]["created_by"]);
                        info.created_when = cn.ConvertNullDateTime(drc[i]["created_when"]);
                        info.updated_by = cn.ConvertNullInt(drc[i]["updated_by"]);
                        info.updated_when = cn.ConvertNullDateTime(drc[i]["updated_when"]);
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_FIM_MAS_ReceiveTypes> GetList(Nullable<int> bgm_source_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_FIM_MAS_ReceiveTypes> infoList = new List<M_FIM_MAS_ReceiveTypes>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@bgm_source_seq", bgm_source_seq);

                strSQL = "SELECT rt.receive_type_seq, rt.receive_type_name"
                    + " FROM fim_mas_receive_types rt"
                    + " WHERE rt.record_status not like @delete_status"
                    + (bgm_source_seq != null ? " AND rt.bgm_source_seq = @bgm_source_seq" : "")
                    + " ORDER BY rt.bgm_source_seq, rt.receive_type_name";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_FIM_MAS_ReceiveTypes info = new M_FIM_MAS_ReceiveTypes();
                        info.receive_type_seq = Convert.ToInt32(drc[i]["receive_type_seq"]);
                        info.receive_type_name = drc[i]["receive_type_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_FIM_MAS_ReceiveTypes GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_FIM_MAS_ReceiveTypes info = new M_FIM_MAS_ReceiveTypes();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@seq", seq);

                strSQL = "SELECT rt.receive_type_seq, rt.receive_type_name"
                    + ", rt.bgm_source_seq, s.bgm_source_name, rt.bank_acc_seq"
                    + ", concat(acc.bank_acc_no, ' : ', acc.bank_acc_name) as bank_acc_name"
                    + ", rt.account_seq, rt.inv_connect_type, rt.remarks"
                    + ", case when rt.inv_connect_type = '0' then ''"
                        + " else case when rt.inv_connect_type = '1' then 'ขายแบบ'"
                            + " else case when rt.inv_connect_type = '2' then 'หลักประกันซอง'"
                                + " else case when rt.inv_connect_type = '3' then 'หลักประกันสัญญา'"
                                    + " else case when rt.inv_connect_type = '4' then 'หลักประกันจ่ายล่วงหน้า'"
                                        + " else case when rt.inv_connect_type = '5' then 'จำหน่ายพัสดุ'"
                                            + " else 'ไม่ระบุ'"
                                            + " end"
                                        + " end"
                                    + " end"
                                + " end"
                            + " end"
                        + " end as inv_connect_type_name"
                    + ", rt.record_status"
                    + ", rt.created_by, rt.created_when, rt.updated_by, rt.updated_when"
                    + " FROM fim_mas_receive_types rt"
                    + " LEFT JOIN bgm_mas_source s ON rt.bgm_source_seq = s.bgm_source_seq"
                    + " LEFT JOIN fim_mas_bank_accounts acc ON rt.bank_acc_seq = acc.bank_acc_seq"
                    + " WHERE rt.receive_type_seq = @seq";

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.receive_type_seq = Convert.ToInt32(drc[0]["receive_type_seq"]);
                    info.receive_type_name = drc[0]["receive_type_name"].ToString();
                    info.bgm_source_seq = cn.ConvertNullInt(drc[0]["bgm_source_seq"]);
                    info.bgm_source_name = drc[0]["bgm_source_name"].ToString();
                    info.bank_acc_seq = cn.ConvertNullInt(drc[0]["bank_acc_seq"]);
                    info.bank_acc_name = drc[0]["bank_acc_name"].ToString();
                    info.account_seq = cn.ConvertNullInt(drc[0]["account_seq"]);
                    info.inv_connect_type = drc[0]["inv_connect_type"].ToString();
                    info.remarks = drc[0]["remarks"].ToString();
                    info.inv_connect_type_name = drc[0]["inv_connect_type_name"].ToString();

                    info.record_status = drc[0]["record_status"].ToString();
                    info.created_by = cn.ConvertNullInt(drc[0]["created_by"]);
                    info.created_when = cn.ConvertNullDateTime(drc[0]["created_when"]);
                    info.updated_by = cn.ConvertNullInt(drc[0]["updated_by"]);
                    info.updated_when = cn.ConvertNullDateTime(drc[0]["updated_when"]);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }

        public void Insert(M_FIM_MAS_ReceiveTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@receive_type_seq", info.receive_type_seq);
                dbManager.AddParameters(1, "@bgm_source_seq", info.bgm_source_seq);
                dbManager.AddParameters(2, "@receive_type_name", info.receive_type_name);
                dbManager.AddParameters(3, "@bank_acc_seq", info.bank_acc_seq);
                dbManager.AddParameters(4, "@inv_connect_type", info.inv_connect_type);
                dbManager.AddParameters(5, "@remarks", info.remarks);

                dbManager.AddParameters(6, "@record_status", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(7, "@created_by", info.created_by);
                dbManager.AddParameters(8, "@created_when", info.created_when);

                strSQL = "INSERT INTO fim_mas_receive_types"
                    + "(bgm_source_seq, receive_type_name"
                    + ", bank_acc_seq, inv_connect_type, remarks"
                    + ", record_status, created_by, created_when)"
                    + " VALUES (@bgm_source_seq"
                        + ", @receive_type_name"
                        + ", @bank_acc_seq"
                        + ", @inv_connect_type"
                        + ", @remarks"
                        + ", @record_status"
                        + ", @created_by"
                        + ", @created_when"
                        + ")";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(M_FIM_MAS_ReceiveTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@receive_type_seq", info.receive_type_seq);
                dbManager.AddParameters(1, "@bgm_source_seq", info.bgm_source_seq);
                dbManager.AddParameters(2, "@receive_type_name", info.receive_type_name);
                dbManager.AddParameters(3, "@bank_acc_seq", info.bank_acc_seq);
                dbManager.AddParameters(4, "@inv_connect_type", info.inv_connect_type);
                dbManager.AddParameters(5, "@remarks", info.remarks);

                dbManager.AddParameters(6, "@updated_by", info.updated_by);
                dbManager.AddParameters(7, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_mas_receive_types"
                    + " SET bgm_source_seq = @bgm_source_seq"
                        + ", receive_type_name = @receive_type_name"
                        + ", bank_acc_seq = @bank_acc_seq"
                        + ", inv_connect_type = @inv_connect_type"
                        + ", remarks = @remarks"
                        + ", updated_by = @Updated_by"
                        + ", updated_when = @Updated_when"
                    + " WHERE receive_type_seq = @receive_type_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(M_FIM_MAS_ReceiveTypes info)
        {
            string strSQL;
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@receive_type_seq", info.receive_type_seq);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", info.updated_by);
                dbManager.AddParameters(3, "@updated_when", info.updated_when);

                strSQL = "UPDATE fim_mas_receive_types"
                    + " SET record_status = @record_status"
                    + ", updated_by = @updated_by"
                    + ", updated_when = @updated_when"
                    + " WHERE receive_type_seq = @receive_type_seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
